<?php 
	jimport('joomla.application.component.controller');
	//require 'components/com_kinh_doanh/tables/kinh_doanh.php';

	
	class sale_tsc_orderControllersu_luoc_ban_hang extends JController
	{    
	   
        //chuyen order den khach
        function chuyen()
        {
            $con = $_SESSION['config'];
            $id_wh = $con->id_warehouse; 
            $model = $this->getModel("order_sp");
            $post = JRequest::get('post',4);
            $id_bill =  $post['id_bill'];
            $db = &JFactory::getDBO();
            $sql = "UPDATE #__erp_sale SET
                                status = 3
                                WHERE 
                                id ='".$id_bill."'";
                                $db->setQuery($sql);
                                $db->query(); 
            $this->setRedirect("index.php?option=com_sale_tsc_order&task=su_luoc_ban_hang","Thanh Cong");
        }	
           
	    //xoa don hang
        function xoa()
        {
            $con = $_SESSION['config'];
            $id_wh = $con->id_warehouse; 
            $model = $this->getModel("order_sp");
            $post = JRequest::get('post',4);
            $id_bill =  $post['id_bill'];
            $model->xoa_buz($id_bill);
            $data = $model->ds_bill($id_bill);
            //print_r($data);
            $model->cap_nhat_stt_sale_order($id_bill);
            foreach($data as $row)
            {
                
                $model->cap_nhat_lai_so_luong($row->id_pro,$id_wh,$row->number);
            }
            $db = &JFactory::getDBO();
             $sql = "UPDATE #__erp_sale SET
                                del = 1, who_del = '".$_SESSION['id_user']."'
                                WHERE 
                                id ='".$id_bill."'";
                                $db->setQuery($sql);
                                $db->query(); 
            $this->setRedirect("index.php?option=com_sale_tsc_order&task=su_luoc_ban_hang","Thanh Cong");
        }
        
	    //nhan vien order them don hang 
        function order_them_dh()
        {
            $post = JRequest::get('post',4);
            //print_r($post);die;
            $flag = 0;$m=0;
            $n = count($post);
            foreach($post as $p)
            {
                if($n != $m-1)
                {
                    if($p>0)
                    {
                        $flag =1;
                    }
                }
                $m++;
                
            }

           
            if($flag==0)
            {
                $this->setRedirect("index.php?option=com_sale_tsc_order&task=order","Loi don hang khong duoc de trong.");return;
            }
            $model = $this->getModel("order_sp");
            $data = $model->danh_sach_san_pham();
            $date = date('Y-m-d H:i:s');
            $ro =  & JTable::getInstance('erp_sale', 'Table'); 
            $con = $_SESSION['config'];
            $id_wh = $con->id_warehouse; 
            $num_order = $con->order_number;
            for($i=1;$i<=$num_order;$i++)
            {
                $ar[$i] = $i; 
                   
            }

            $buz = $model->lay_buz();
            $ar2 = array();
            foreach($buz as $bu)
            {
                $ar2[$bu->id_order] = $bu->id_order;    
            }
            $rs = array_diff($ar,$ar2);
            $x = array_rand($rs);
            $b =  & JTable::getInstance('erp_sale_order_buzy', 'Table'); 
            $b->id_order = $x;
            $b->time = $date;
             if (!$b->store()) { //thuc thi them record  neu co loi se hien thi
                              	     //JError::raiseError(500, $row->getError() );
                                     $this->order_them_dh();
                                        }
            else
            {
                $b->time = $date;
                $ro =  & JTable::getInstance('erp_sale', 'Table'); 
                $ro->id_order=$x;
                $ro->id_employee = $_SESSION['id_user'];
                $ro->date = $date;
                $ro->status = 1;
                if (!$ro->store()) { //thuc thi them record  neu co loi se hien thi
                                  	     JError::raiseError(500, $row->getError() );
                                            }
                foreach($data as $row)
                {
                    if($_POST['sp'.$row->id_pro] > 0)
                    {
                        $r =  & JTable::getInstance('erp_sale_order', 'Table'); 
                        $r->id_order = $ro->id;
                        $r->id_product = $row->id_pro;
                        $r->number = $_POST['sp'.$row->id_pro];
                        $r->status = 0;
                        if (!$r->store()) { //thuc thi them record  neu co loi se hien thi
                                  	     JError::raiseError(500, $row->getError() );
                                            }
                        $model->giam_sl_theo_order($row->id_pro,$id_wh,$_POST['sp'.$row->id_pro]);
                    }
                }
            }
            //echo "ssss";
            $this->setRedirect("index.php?option=com_sale_tsc_order&task=su_luoc_ban_hang","Thanh Cong");
            //$this->setRedirect("index.php?option=com_kinh_doanh&task=orther_sp","thanh cong");   
        }
        
        //order san pham
        function order()
        {
            JRequest::setVar('view','order_sp'); 
			parent::display();	 // hien thi
        }
        
        //neu khong co task nao phu hop thi mac se chay function nay
		function display()
		{
			JRequest::setVar('view','su_luoc_ban_hang'); // goi view dang nhap
			parent::display();	 // hien thi
		}     

        function su_luoc_ban_hang()
		{
			JRequest::setVar('view','su_luoc_ban_hang'); // goi view dang nhap
			parent::display();	 // hien thi
		}     
    }
?>